package com.xydtech.workflow.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.workflow.domain.WflPageTemplate;
import com.xydtech.workflow.service.IWflPageTemplateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程页面模板Controller
 *
 * @author JHON
 * @date 2021-12-21
 */
@Controller
@RequestMapping("/workflow/pageTemplate")
public class WflPageTemplateController extends BaseController {
    private String prefix = "workflow/pagetemplate";

    @Autowired
    private IWflPageTemplateService wflPageTemplateService;

    @RequiresPermissions("workflow:pageTemplate:view")
    @GetMapping()
    public String pageTemplate() {
        return prefix + "/pageTemplate";
    }

    /**
     * 查询流程页面模板列表
     */
    @RequiresPermissions("workflow:pageTemplate:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WflPageTemplate wflPageTemplate) {
        startPage();
        List<WflPageTemplate> list = wflPageTemplateService.selectWflPageTemplateList(wflPageTemplate);
        return getDataTable(list);
    }

    /**
     * 导出流程页面模板列表
     */
    @RequiresPermissions("workflow:pageTemplate:export")
    @Log(title = "流程页面模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WflPageTemplate wflPageTemplate) {
        List<WflPageTemplate> list = wflPageTemplateService.selectWflPageTemplateList(wflPageTemplate);
        ExcelUtil<WflPageTemplate> util = new ExcelUtil<WflPageTemplate>(WflPageTemplate.class);
        return util.exportExcel(list, "流程页面模板数据");
    }

    /**
     * 新增流程页面模板
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存流程页面模板
     */
    @RequiresPermissions("workflow:pageTemplate:add")
    @Log(title = "流程页面模板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WflPageTemplate wflPageTemplate) {
        return toAjax(wflPageTemplateService.insertWflPageTemplate(wflPageTemplate));
    }

    /**
     * 修改流程页面模板
     */
    @GetMapping("/edit/{pageId}")
    public String edit(@PathVariable("pageId") Long pageId, ModelMap mmap) {
        WflPageTemplate wflPageTemplate = wflPageTemplateService.selectWflPageTemplateByPageId(pageId);
        mmap.put("wflPageTemplate", wflPageTemplate);
        return prefix + "/edit";
    }

    /**
     * 修改保存流程页面模板
     */
    @RequiresPermissions("workflow:pageTemplate:edit")
    @Log(title = "流程页面模板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WflPageTemplate wflPageTemplate) {
        return toAjax(wflPageTemplateService.updateWflPageTemplate(wflPageTemplate));
    }

    /**
     * 删除流程页面模板
     */
    @RequiresPermissions("workflow:pageTemplate:remove")
    @Log(title = "流程页面模板", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wflPageTemplateService.deleteWflPageTemplateByPageIds(ids));
    }
}
